package com.leo.oplayer.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.leo.oplayer.dao.DBService;
import com.leo.oplayer.model.ConDetail;
import com.leo.oplayer.model.MonthStat;
import com.leo.oplayer.model.WhitePerson;
import com.leo.oplayer.util.Constancts;
import com.leo.oplayer.util.DateUtil;

public class ContactService {
	
	public static final String TAG = "ConatactService";
	
	private DBService dbService;
	
	public ContactService(){}
	
	public ContactService(Context context){
		dbService = new DBService(context);
	}
	
	public ContactService(DBService dbService){
		this.dbService = dbService;
	}
	
	/**
	 * 同步通信数据
	 * @param phoneId
	 * @param action
	 */
	public void syncCommunicationStat(String phoneId,ConDetail detail,int action){
		MonthStat stat = new MonthStat(phoneId,DateUtil.getCurrentMonth());
		String column = null;
	
		switch (action){
		case Constancts.CALL_IN:
			stat.setInCallNum(1);
			stat.setPoint(Constancts.POINT_TELE);
			column = MonthStat._IN_CALL_NUM;
			detail.setFlag(ConDetail.TEL_CALL);
			break;
		case Constancts.CALL_OUT:
			stat.setOutCallNum(1);
			stat.setPoint(Constancts.POINT_TELE);
			column = MonthStat._OUT_CALL_NUM;
			detail.setFlag(ConDetail.TEL_CALL);
			break;
		case Constancts.MSG_IN:
			stat.setInMsgNum(1);
			stat.setPoint(Constancts.POINT_SMS);
			column = MonthStat._IN_MSG_NUM;
			detail.setFlag(ConDetail.TEL_MSG);
			break;
		case Constancts.MSG_OUT:
			stat.setOutMsgNum(1);
			stat.setPoint(Constancts.POINT_SMS);
			column = MonthStat._OUT_MSG_NUM;
			detail.setFlag(ConDetail.TEL_MSG);
			break;
		default:
			return;
		}
		
		MonthStat dbStat = dbService.selectStatById(DateUtil.getCurrentMonth(), phoneId);		
		if(dbStat == null){
			dbService.insertMonthStat(stat);
		}else{
			dbService.updateMonthStat(stat, column);
		}
		detail.setPhoneId(phoneId);
		dbService.insertDetail(detail);
	}
	
	/**
	 * 查询统计列表
	 * @param month
	 * @return list
	 */
	public List<MonthStat> searchStatList(String month){
		return dbService.searchStatList(month);
	}
	
	/**
	 * 查询与某人通讯详情列表
	 * @param detail
	 * @return List
	 */
	public List<ConDetail> searchDetailList(ConDetail detail){
		return dbService.searchDetailList(detail);
	}
	
	/**
	 * 轮询检查，找出长时间没联系超过系统设置的白名单人员
	 * @return
	 */
	public List<String> checkLongNoContacts(int spanDay){
		List<WhitePerson> persons = dbService.searchWhiteList(null);
		List<String> retPersons = new ArrayList<String>();
		if(persons != null && !persons.isEmpty()){
			//轮询白名单
			for(WhitePerson person : persons){			
				String latestTime = dbService.getLatestTime(person.getPersonId());
				if(latestTime != null){
					if(DateUtil.overMaxSpan(latestTime.substring(0, 8), spanDay)){
						retPersons.add(person.getPersonId());
					}
				}
			}
		}
		return retPersons;
	}
	
	
	
	public DBService getDbService() {
		return dbService;
	}

	//TODO:未完成
	public void insertWhiteList(String phoneId){
		
	}
	
	//TODO:未完成
	public void deleteWhitePerson(String phoneId){
		
	}

}
